/**
 * 通用类型定义
 */

// API响应通用结构
export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
  timestamp: number
}

// 分页查询参数
export interface PageQuery {
  current: number
  size: number
  sortField?: string
  sortOrder?: 'asc' | 'desc'
}

// 分页结果
export interface PageResult<T> {
  current: number
  size: number
  total: number
  pages: number
  records: T[]
}

// 用户信息
export interface User {
  id: number
  username: string
  realName: string
  email: string
  phone: string
  avatar?: string
  roles: string[]
  permissions: string[]
  status: 'ACTIVE' | 'DISABLED'
  createTime: string
  updateTime: string
}

// 登录表单
export interface LoginForm {
  username: string
  password: string
  remember?: boolean
}

// 登录响应
export interface LoginResponse {
  token: string
  user: User
}

// 菜单项
export interface MenuItem {
  id: string
  key: string
  title: string
  icon?: string
  path?: string
  children?: MenuItem[]
  permissions?: string[]
}

// 数据宽表
export interface WideTable {
  id: number
  name: string
  displayName?: string
  description: string
  type: 'ONLINE' | 'OFFLINE'
  fields: WideTableField[]
  dataSource?: string
  esIndexName?: string
  status: 'ACTIVE' | 'INACTIVE'
  dataCount?: number
  createTime: string
  updateTime: string
  creator: string
}

// 数据宽表字段
export interface WideTableField {
  id: number
  name: string
  displayName: string
  type: 'STRING' | 'NUMBER' | 'DATE' | 'BOOLEAN'
  length?: number
  precision?: number
  nullable: boolean
  defaultValue?: any
  description?: string
  isDisplayField: boolean
  isQueryField: boolean
  sortOrder: number
}

// 数据宽表数据视图对象
export interface WideTableDataVO {
  // 数据列表
  records: Record<string, any>[]
  // 字段信息
  fieldInfos: WideTableFieldInfo[]
  // 总记录数
  total: number
  // 当前页
  current: number
  // 每页大小
  size: number
  // 查询耗时(毫秒)
  queryTime: number
  // 聚合结果
  aggregations?: Record<string, any>
}

// 宽表字段信息
export interface WideTableFieldInfo {
  // 字段名称
  fieldName: string
  // 显示名称
  displayName: string
  // 字段类型
  fieldType: string
  // 是否为显示字段
  isDisplayField: boolean
  // 是否为查询字段
  isQueryField: boolean
  // 排序序号
  sortOrder: number
  // 字段统计信息
  stats?: WideTableFieldStats
}

// 字段统计信息
export interface WideTableFieldStats {
  // 非空值数量
  count: number
  // 唯一值数量
  distinctCount: number
  // 最小值
  min?: any
  // 最大值
  max?: any
  // 平均值(数值类型)
  avg?: number
  // 总和(数值类型)
  sum?: number
  // 空值数量
  nullCount: number
  // 空值比例
  nullRate: number
}

// 组件
export interface Component {
  id: number
  name: string
  version: string
  description?: string
  type: 'DATA' | 'CHART'
  templateId: number
  templateName: string
  config: ComponentConfig
  previewUrl?: string
  status: 'DRAFT' | 'SUBMITTED' | 'APPROVED' | 'REJECTED' | 'PUBLISHED'
  createTime: string
  updateTime: string
  creator: string
}

// 组件配置
export interface ComponentConfig {
  wideTableId: number
  displayFields: string[]
  queryFields: string[]
  calculationRules: CalculationRule[]
  chartType?: string
  chartConfig?: ChartConfig
  updateSchedule?: UpdateSchedule
}

// 计算规则
export interface CalculationRule {
  id: string
  name: string
  expression: string
  type: 'SUM' | 'AVG' | 'COUNT' | 'MAX' | 'MIN' | 'CUSTOM'
  fields: string[]
}

// 图表配置
export interface ChartConfig {
  type: 'line' | 'bar' | 'pie' | 'scatter' | 'table'
  title?: string
  legend?: boolean
  grid?: any
  xAxis?: any
  yAxis?: any
  series?: any[]
  color?: string[]
}

// 更新计划
export interface UpdateSchedule {
  type: 'MANUAL' | 'DAILY' | 'WEEKLY' | 'MONTHLY'
  time?: string
  enabled: boolean
}

// 组件模板
export interface ComponentTemplate {
  id: number
  name: string
  type: 'DATA' | 'CHART'
  category: string
  description?: string
  previewUrl?: string
  config: any
  createTime: string
}

// 主题场景
export interface ThemeScenario {
  id: number
  name: string
  displayName: string
  description?: string
  scenarioType: 'DASHBOARD' | 'REPORT' | 'ANALYSIS' | 'MONITOR' | 'PRESENTATION' | 'INTERACTIVE' | 'REALTIME' | 'CUSTOM'
  applicationType: 'PC' | 'MOBILE' | 'PAD' | 'LARGE_SCREEN' | 'TV' | 'UNIVERSAL'
  templateId: number
  templateName: string
  layoutConfig?: string
  themeConfig?: string
  componentsConfig?: string
  interactionConfig?: string
  dataConfig?: string
  styleConfig?: string
  cacheConfig?: string
  refreshConfig?: string
  permissionConfig?: string
  previewUrl?: string
  thumbnailUrl?: string
  accessPath?: string
  isPublic: boolean
  isRecommended: boolean
  accessCount: number
  rating: number
  tags?: string[]
  status: 'DRAFT' | 'SUBMITTED' | 'APPROVED' | 'REJECTED' | 'PUBLISHED' | 'ARCHIVED' | 'DELETED'
  publishTime?: string
  creator: string
  createTime: string
  updateTime: string
}

// 主题组件
export interface ThemeComponent {
  id: string
  componentId: number
  componentName: string
  version: string
  position: {
    x: number
    y: number
    width: number
    height: number
  }
  config: any
}

// 主题配置
export interface ThemeConfig {
  layout: 'grid' | 'free'
  background?: string
  theme?: string
  responsive?: boolean
}

// 审批流程
export interface ApprovalProcess {
  id: number
  businessProcessId: string
  externalProcessId: string
  processDefinitionKey: string
  processDefinitionName: string
  processVersion: string
  businessType: 'COMPONENT_APPROVAL' | 'THEME_SCENARIO_APPROVAL' | 'TEMPLATE_APPROVAL' | 'USER_PERMISSION_APPROVAL' | 'DATA_ACCESS_APPROVAL' | 'CUSTOM_APPROVAL'
  businessObjectId: number
  businessObjectName: string
  businessData?: string
  status: 'DRAFT' | 'SUBMITTED' | 'IN_PROGRESS' | 'APPROVED' | 'REJECTED' | 'CANCELLED' | 'TIMEOUT' | 'ERROR'
  currentStep: number
  totalSteps: number
  currentNodeId?: string
  currentNodeName?: string
  currentApprover?: string
  currentApproverName?: string
  submitter: string
  submitterName: string
  submitTime: string
  completeTime?: string
  timeoutTime?: string
  processVariables?: string
  approvalComment?: string
  priority: 'LOW' | 'NORMAL' | 'HIGH' | 'URGENT'
  workflowConfigId: number
  createTime: string
  updateTime: string
}

// 审批历史
export interface ApprovalHistory {
  id: number
  processId: number
  externalProcessId: string
  externalTaskId?: string
  step: number
  nodeId: string
  nodeName: string
  nodeType: 'START' | 'USER_TASK' | 'SERVICE_TASK' | 'GATEWAY' | 'END' | 'SUB_PROCESS'
  action: 'SUBMIT' | 'APPROVE' | 'REJECT' | 'DELEGATE' | 'TRANSFER' | 'WITHDRAW' | 'COMPLETE' | 'CANCEL' | 'TIMEOUT' | 'SYSTEM'
  result: 'SUCCESS' | 'FAILED' | 'PENDING' | 'TIMEOUT' | 'ERROR'
  comment?: string
  attachments?: string
  operator: string
  operatorName: string
  operatorDepartment?: string
  delegate?: string
  delegateName?: string
  operateTime: string
  duration?: number
  nextApprover?: string
  nextApproverName?: string
  variableChanges?: string
  extendInfo?: string
  createTime: string
}

// 组件库
export interface ComponentLibrary {
  id: number
  name: string
  displayName: string
  description?: string
  componentType: 'DATA_COMPONENT' | 'CHART_COMPONENT' | 'COMPOSITE_COMPONENT'
  componentId: number
  categoryId?: number
  categoryName?: string
  libraryType: 'TEMPLATE' | 'CUSTOM' | 'SYSTEM' | 'SHARED'
  version: string
  tags?: string
  isPublic: boolean
  isRecommended: boolean
  usageCount: number
  rating: number
  previewUrl?: string
  thumbnailUrl?: string
  status: 'DRAFT' | 'PUBLISHED' | 'ARCHIVED' | 'DELETED'
  creator: string
  createTime: string
  updateTime: string
}

// 组件分类
export interface ComponentCategory {
  id: number
  name: string
  displayName: string
  description?: string
  parentId?: number
  path?: string
  level: number
  icon?: string
  color?: string
  sortOrder?: number
  componentType: 'ALL' | 'DATA_COMPONENT' | 'CHART_COMPONENT' | 'COMPOSITE_COMPONENT'
  isLeaf: boolean
  status: 'ACTIVE' | 'INACTIVE' | 'DELETED'
  creator: string
  createTime: string
  updater?: string
  updateTime: string
  children?: ComponentCategory[]
}

// 组件库查询参数
export interface ComponentLibraryQuery extends PageQuery {
  keyword?: string
  componentType?: string
  categoryId?: number
  libraryType?: string
  status?: string
  isRecommended?: boolean
  tags?: string[]
}

// 组件库统计
export interface ComponentLibraryStats {
  totalComponents: number
  dataComponents: number
  chartComponents: number
  totalUsage: number
  averageRating: number
}

// 主题场景组件
export interface ThemeScenarioComponent {
  id: number
  scenarioId: number
  componentKey: string
  componentType: 'DATA_COMPONENT' | 'CHART_COMPONENT' | 'TEXT_COMPONENT' | 'IMAGE_COMPONENT' | 'VIDEO_COMPONENT' | 'MAP_COMPONENT' | 'TABLE_COMPONENT' | 'FORM_COMPONENT' | 'CONTAINER_COMPONENT' | 'DECORATION_COMPONENT' | 'CUSTOM_COMPONENT'
  sourceComponentId: number
  componentName: string
  displayName: string
  version: string
  positionConfig?: string
  styleConfig?: string
  dataConfig?: string
  interactionConfig?: string
  animationConfig?: string
  permissionConfig?: string
  customConfig?: string
  zIndex: number
  visible: boolean
  locked: boolean
  parentId?: number
  groupKey?: string
  sortOrder: number
  createTime: string
  updateTime: string
}

// 主题场景模板
export interface ThemeScenarioTemplate {
  id: number
  name: string
  displayName: string
  description?: string
  category: string
  scenarioType: 'DASHBOARD' | 'REPORT' | 'ANALYSIS' | 'MONITOR' | 'PRESENTATION' | 'INTERACTIVE' | 'REALTIME' | 'CUSTOM'
  applicationType: 'PC' | 'MOBILE' | 'PAD' | 'LARGE_SCREEN' | 'TV' | 'UNIVERSAL'
  layoutType: 'GRID' | 'FREE' | 'FLOW' | 'TABS' | 'ACCORDION' | 'SPLIT' | 'FIXED' | 'RESPONSIVE'
  defaultLayoutConfig?: string
  defaultThemeConfig?: string
  defaultStyleConfig?: string
  presetComponentsConfig?: string
  previewUrl?: string
  thumbnailUrl?: string
  templateConfig?: string
  isSystemTemplate: boolean
  isRecommended: boolean
  usageCount: number
  rating: number
  tags?: string[]
  sortOrder?: number
  status: 'DRAFT' | 'ACTIVE' | 'INACTIVE' | 'DEPRECATED' | 'DELETED'
  creator: string
  createTime: string
  updateTime: string
}

// 主题场景查询参数
export interface ThemeScenarioQuery extends PageQuery {
  keyword?: string
  scenarioType?: string
  applicationType?: string
  status?: string
  isRecommended?: boolean
  creator?: string
  tags?: string[]
}

// 主题场景统计
export interface ThemeScenarioStats {
  totalScenarios: number
  publishedScenarios: number
  draftScenarios: number
  totalAccess: number
  averageRating: number
  topScenarios: ThemeScenario[]
}

// 工作流配置
export interface WorkflowConfig {
  id: number
  name: string
  code: string
  engineType: 'CSII_WORKFLOW' | 'ACTIVITI' | 'FLOWABLE' | 'CAMUNDA' | 'CUSTOM'
  engineUrl: string
  authConfig?: string
  connectionConfig?: string
  mappingConfig?: string
  enabled: boolean
  isDefault: boolean
  timeout: number
  retryCount: number
  description?: string
  creator: string
  createTime: string
  updateTime: string
}

// 审批查询参数
export interface ApprovalQuery extends PageQuery {
  keyword?: string
  businessType?: string
  status?: string
  priority?: string
  submitter?: string
  currentApprover?: string
  startTime?: string
  endTime?: string
}

// 审批统计
export interface ApprovalStats {
  totalProcesses: number
  pendingProcesses: number
  completedProcesses: number
  timeoutProcesses: number
  averageProcessTime: number
  myPendingTasks: number
  myCompletedTasks: number
  myStartedProcesses: number
}

// 流程定义
export interface ProcessDefinition {
  id: string
  key: string
  name: string
  version: number
  description?: string
  category?: string
  deploymentTime: string
  suspended: boolean
}

// 数据组件相关类型定义
export interface DataComponent {
  id: number
  name: string
  displayName: string
  description?: string
  type: 'TABLE' | 'LIST' | 'CARD' | 'TREE' | 'FORM' | 'DETAIL' | 'SEARCH' | 'FILTER'
  dataSourceType: 'WIDE_TABLE' | 'DATABASE' | 'ELASTICSEARCH' | 'API' | 'FILE'
  dataSourceConfig?: string
  wideTableId?: number
  sqlQuery?: string
  esQuery?: string
  apiConfig?: string
  fieldMapping?: string
  cacheConfig?: string
  refreshInterval?: number
  status: 'DRAFT' | 'ACTIVE' | 'INACTIVE' | 'DELETED'
  creator: string
  createTime: string
  updater?: string
  updateTime: string
  version: number
}

export interface DataComponentField {
  id?: number
  componentId: number
  fieldName: string
  displayName: string
  fieldType: 'STRING' | 'NUMBER' | 'DATE' | 'DATETIME' | 'BOOLEAN' | 'ENUM' | 'JSON' | 'IMAGE' | 'LINK' | 'MONEY' | 'PERCENT' | 'TAGS'
  formatRule?: string
  width?: number
  visible?: boolean
  searchable?: boolean
  sortable?: boolean
  filterable?: boolean
  align?: 'LEFT' | 'CENTER' | 'RIGHT'
  renderer?: string
  validationRule?: string
  defaultValue?: string
  description?: string
  sortOrder?: number
  createTime?: string
  updateTime?: string
}

// 图表组件相关类型定义
export interface ChartComponent {
  id: number
  name: string
  displayName: string
  description?: string
  chartType: 'LINE' | 'BAR' | 'PIE' | 'SCATTER' | 'AREA' | 'GAUGE' | 'RADAR' | 'FUNNEL' | 'TREEMAP' | 'SUNBURST' | 'HEATMAP' | 'CANDLESTICK' | 'BOXPLOT' | 'PARALLEL' | 'SANKEY' | 'GRAPH' | 'WORDCLOUD' | 'LIQUIDFILL' | 'CUSTOM'
  dataSourceType: 'WIDE_TABLE' | 'DATA_COMPONENT' | 'DATABASE' | 'ELASTICSEARCH' | 'API' | 'FILE'
  dataSourceConfig?: string
  wideTableId?: number
  dataComponentId?: number
  sqlQuery?: string
  esQuery?: string
  chartConfig?: string
  dataConfig?: string
  styleConfig?: string
  interactionConfig?: string
  cacheConfig?: string
  refreshInterval?: number
  previewUrl?: string
  status: 'DRAFT' | 'ACTIVE' | 'INACTIVE' | 'DELETED'
  creator: string
  createTime: string
  updater?: string
  updateTime: string
  version: number
}